﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;

namespace ParangonBiz.SiteMapDataProvider
{
    public class AxSiteMapDataProvider : StaticSiteMapProvider
    {
        private SiteMapNode _mRootNode;

        public AxSiteMapDataProvider()
        {

        }

        // create the root node
        public override void Initialize(string name, NameValueCollection attributes)
        {
            base.Initialize(name, attributes);
            _mRootNode = new SiteMapNode(this, "Home", "/Default.aspx", "Главная");
            AddNode(_mRootNode);
        }

        public override SiteMapNode BuildSiteMap()
        {
            return _mRootNode;
        }

        public override SiteMapNode RootNode
        {
            get
            {
                return _mRootNode;
            }
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return RootNode;
        }

        public override SiteMapNode FindSiteMapNode(string rawUrl)
        {
            return base.FindSiteMapNode(rawUrl);
        }

        // stack a node under the root
        public SiteMapNode Stack(string title, string uri)
        {
            return Stack(title, uri, _mRootNode);
        }

        // stack a node under any other node
        public SiteMapNode Stack(string title, string uri, SiteMapNode parentnode)
        {
            lock (this)
            {
                SiteMapNode node = base.FindSiteMapNodeFromKey(uri);

                if (node == null)
                {
                    node = new SiteMapNode(this, uri, uri, title) {ParentNode = (parentnode ?? _mRootNode)};
                    AddNode(node);
                }
                else if (node.Title != null && node.Title != title)
                {
                    node.Title = title;
                }

                return node;
            }
        }

        public void Stack(List<KeyValuePair<string, Uri>> nodes)
        {
            SiteMapNode parent = RootNode;
            foreach (KeyValuePair<string, Uri> node in nodes)
            {
                parent = Stack(node.Key, node.Value.PathAndQuery, parent);
            }
        }
    }

}
